package com.sxbang.bms.service.impl;

import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dao.RoleDao;
import com.sxbang.bms.dto.RoleDto;
import com.sxbang.bms.model.SysRole;
import com.sxbang.bms.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@Autowired
	private RoleDao roleDao;

	@Override
	public List<SysRole> getAllRoles() {
		return roleDao.getAllRoles();
	}

	@Override
	public int saveRole(RoleDto roleDto) {
		log.info(getClass().getName() + ".saveRole() 1、先保存角色");
		roleDao.save(roleDto);
		List<Long> permissionIds = roleDto.getPermissionIds();
		permissionIds.remove(0L);
		log.info(getClass().getName() + ".saveRole() 2、插入权限");
		if (!CollectionUtils.isEmpty(permissionIds)) {
			roleDao.saveRolePermission(roleDto.getId(), permissionIds);
		}
		return 0;
	}

	@Override
	@Transactional
	public int updateRole(RoleDto roleDto) {
		List<Long> permissionIds = roleDto.getPermissionIds();
		permissionIds.remove(0L);
		log.info(getClass().getName() + ".deleteRolePermission() 1、更新角色权限之前要删除该角色之前的所有权限");
		roleDao.deleteRolePermission(roleDto.getId());

		log.info(getClass().getName() + ".deleteRolePermission() 2、判断该角色是否有赋予权限值，有就添加");
		if (!CollectionUtils.isEmpty(permissionIds)) {
			roleDao.saveRolePermission(roleDto.getId(), permissionIds);
		}
		return roleDao.updateRole(roleDto);
	}

	@Override
	public SysRole getRole(String name) {
		return roleDao.getRole(name);
	}

	@Override
	public SysRole getRoleById(Integer id) {
		return roleDao.getById(id);
	}

	@Override
	public int deleteRole(Integer id) {
		return roleDao.deleteRole(id);
	}

	@Override
	public SysRole save(SysRole sysRole) {
		return null;
	}

	@Override
	public Results<SysRole> getByPage(Integer offset, Integer limit) {
		return Results.success(roleDao.countAll().intValue(), roleDao.getByPage(offset, limit));
	}

	@Override
	public List<SysRole> save(List<SysRole> sysRoles) {
		return null;
	}

	@Override
	public SysRole get(Integer integer) {
		return null;
	}

	@Override
	public List<SysRole> findAll() {
		return null;
	}

	@Override
	public List<SysRole> list(Iterable<Integer> integers) {
		return null;
	}

	@Override
	public List<SysRole> list(Integer offset, Integer limit) {
		return null;
	}

	@Override
	public SysRole update(SysRole e) {
		return null;
	}

	@Override
	public void delete(SysRole sysRole) {
		roleDao.deleteRole(sysRole.getId());
	}

	@Override
	public void delete(Integer integer) {
		roleDao.deleteRole(integer);
	}

	@Override
	public Long countAll() {
		return roleDao.countAll();
	}

	@Override
	public Results<SysRole> getRoleByFuzzyRoleNamePage(String roleName, Integer startPosition, Integer limit) {
		return Results.success(roleDao.countRoleByFuzzyRoleName(roleName).intValue(), roleDao.getRoleByFuzzyRoleNamePage(roleName,startPosition, limit));
	}
}
